﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController
    {
        [ChildActionOnly]
        public ActionResult SSTKRTP(string returnUrl)
        {

            ModelState.Clear();

            MABF.CWBB.SLO = "01";
            MABF.CWBB.SLB = "PB";
            MABF.CWBB.LO = "01";
            MABF.CWBB.LB = "PB";
            MABF.CWBB.LP = "01    ";
            MABF.CWBB.WHP = MABF.CWBB.LO + MABF.CWBB.LB + MABF.CWBB.LP;
            MABF.TERMNO = TERMNO;
            MABF.STAFF = CUSER;
            return PartialView(MABF.CWBB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SSTKRTP(WBBSTModel model, string returnUrl)
        {
            try
            {
                SETTPGEBNR(model);//指定布卷的托盘编号   
                ModelState.Clear();
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void SETTPGEBNR(WBBSTModel model)
        {
            if (model == null)
                return;
            if (model.WBBSTKNR == null)
                return;

            MABF.CWBB.WBBSTKNR = Helper.Left(model.WBBSTKNR.PadRight(12), 12);
            dwData = VHP.VWBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBSTKNR"] = MABF.CWBB.WBBSTKNR.PadRight(12); ;
            SResult = VHP.VWBB.QWB13(dwData);
            DataRow dwWB = null;
            foreach (DataRow row in SResult.Tables[0].Copy().Rows)
            {
                dwWB = row;
                break;
            }
            if (dwWB == null)
            {
                INFO = "布卷不存在或者品种信息不一致";
                return;
            }
            Helper.GetSTime(ref date, ref time, ref stime);
            dwWB["WBBAENDBEN"] = MABF.NEWUSER.PadRight(8);
            dwWB["WBBAENDDAT"] = date.PadRight(8);
            dwWB["WBBGEBNR"] = MABF.CWBB.GEBNR.PadRight(8);
            var result = MABF.ABPS.Where(p => p.ART == dwWB["WBBDISPOART"].ToString() 
            && p.VAR.Substring(0,2) == dwWB["WBBDISPOVAR"].ToString().Substring(0,2) 
            && p.FARBE == dwWB["WBBFARBE"].ToString());
            bool IsFound = false;
            foreach (ABPOSModel abp in result)
            {
                model.GEBNR= MABF.CWBB.GEBNR.PadRight(8);
                model.ABPAUF = abp.ABPAUF;
                IsFound = true;
            }
            if(!IsFound)
            {
                INFO = "不在范围内";
                MABF.CWBB.WBBSTKNR = MABF.CWBB.WBBSTKNR+ "不在范围内";
            }
            else
            {
                VHP.VWBB.SETGEBNR(dwWB, MABF.CWBB.LO, MABF.CWBB.LB, MABF.CWBB.LP, RUNID); 
                MABF.CWBB.WBBSTKNR = "";
                PB2WZ18(model);
            }
        }
        public void PB2WZ18(WBBSTModel model)
        {
            if (model == null)
                return;
            if (model.WBBSTKNR == null)
                return;
            if (model.ABPAUF == "")
            {
                INFO = "不在范围内";
                return;
            }
            model.WBBSTKNR = model.WBBSTKNR.Trim().PadRight(12);
            dwData = VHP.VWBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBAUFTYP"] = "V";
            dwData["WBBAUFKRS"] = model.ABPAUF.Substring(0, 2);
            dwData["WBBAUFLFD"] = model.ABPAUF.Substring(2, 6);
            dwData["WBBAUFPOS"] = model.ABPAUF.Substring(8, 3);
            dwData["WBBSTKNR"] = model.WBBSTKNR.Trim().PadRight(12);
            SResult = VHP.VWBB.QWB24(dwData);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                MWB.IsDataValid = false;
                INFO = "不在范围内";
                return;
            }
            MWB.IsDataValid = true;
            DataRow _dwWB = SResult.Tables[0].Copy().Rows[0];
            double mtr = Convert.ToDouble(_dwWB["WBBISTKLG"].ToString()) * 0.001;
            Helper.GetSTime(ref date, ref time, ref stime);
            _dwWB["WBBAUFTYP"] = "V";
            _dwWB["WBBAUFKRS"] = model.ABPAUF.Substring(0, 2);
            _dwWB["WBBAUFLFD"] = model.ABPAUF.Substring(2, 6);
            _dwWB["WBBAUFPOS"] = model.ABPAUF.Substring(8, 3);
            _dwWB["WBBTP"] = model.GEBNR.Substring(4,4);
            _dwWB["WBBFOLGE"] = Helper.Right("01", 2);
            _dwWB["WBBGEBNR"] = model.GEBNR;
            _dwWB["WBBAENDBEN"] = CUSER;
            _dwWB["WBBAENDDAT"] = date;
            PM["WBROW"] = _dwWB;
            PM["RUNID"] = RUNID;
            VHP.VWBB.WBB61(PM);//先留位，再转移  
        }
    }
}
